import { Mutation, Action } from 'vuex';
import { StoreModuleType } from "@/utils/store";
import { ResponseData } from '@/utils/request';
import { TableDataType, TableListItem, TableListQueryParams } from './data.d';
import {
	queryList,
	removeData,
	createData,
	detailData,
	updateData,
	queryRoleData
} from './service';


export interface StateType {
	tableData: TableDataType;
	updateData: Partial<TableListItem>;
}

export interface ModuleType extends StoreModuleType<StateType> {
	state: StateType;
	mutations: {
		setTableData: Mutation<StateType>;
		setUpdateData: Mutation<StateType>;
	};
	actions: {
		queryTableData: Action<StateType, StateType>;
		deleteTableData: Action<StateType, StateType>;
		createTableData: Action<StateType, StateType>;
		queryUpdateData: Action<StateType, StateType>;
		updateTableData: Action<StateType, StateType>;
		queryRoleTableData: Action<StateType, StateType>;
		detailTableData:Action<StateType, StateType>;
	};
}
const initState: StateType = {
	tableData: {
		list: [],
		pagination: {
			total: 0,
			current: 1,
			pageSize: 10,
			showSizeChanger: true,
			showQuickJumper: true,
		},
	},
	updateData: {},
};

const StoreModel: ModuleType = {
	namespaced: true,
	name: 'ModuleTable',
	state: {
		...initState
	},
	mutations: {
		setTableData(state, payload) {
			state.tableData = payload;
		},
		setUpdateData(state, payload) {
			state.updateData = payload;
		},
	},
	actions: {
		async queryTableData({ commit }, payload: TableListQueryParams) {
			try {
				const response: ResponseData = await queryList(payload);
				commit('setTableData', {
					list: response,

				});
				return true;
			} catch (error) {
				return false;
			}
		},
		async deleteTableData({ commit }, payload: number) {
			try {

				await removeData(payload);
				return true;
			} catch (error) {
				return false;
			}
		},
		async detailTableData({ commit }, payload: number) {
			try {
		
				return  await detailData(payload);
			} catch (error) {
				return null;
			}
		},
		async createTableData({ commit }, payload: Pick<TableListItem, "name" | "desc" | "href" | "type">) {
			try {
				await createData(payload);
				return true;
			} catch (error) {
				return false;
			}
		},
		async queryUpdateData({ commit }, payload: number) {
			try {
				const response: ResponseData = await detailData(payload);
				const { data } = response;
				commit('setUpdateData', {
					...initState.updateData,
					...data,
				});
				return true;
			} catch (error) {
				return false;
			}
		},
		async updateTableData({ commit }, payload: TableListItem) {
			try {

				await updateData(payload);
				return true;
			} catch (error) {
				return false;
			}
		},
		async queryRoleTableData({ commit }, payload: number) {
			try {

				await queryRoleData(payload);
				return true;
			} catch (error) {
				return false;
			}
		},
	}
};

export default StoreModel;
